package webshop.controller.action;

import webshop.model.bl.CustomerManager;
import webshop.model.db.Customer;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6981891508003699429L;
	
	private String username = "";
	private String password = "";
	private String firstname;
	private String lastname;
	
	@Override
	public String execute() throws Exception {
		Customer customer = null;
		try {
			customer = new CustomerManager().getCustomer(username, password);
		} catch (Throwable ex) {
			System.out.println("Fehler: " + ex);
		}
		if (customer == null) {
			addActionError(getText("error.username.register"));
			return "input";
		}
		firstname = customer.getFirstname();
		lastname = customer.getLastname();
		
		System.out.println("customer: " + customer.getFirstname() + " " + customer.getLastname());
		System.out.println("customer is admin: " + customer.isAdmin());
		
		if (customer.isAdmin()) {
			return "successAdmin";
		} else {
			return "successUser";
		}		
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getUsername() {
		return this.username;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

}
